from fastapi import APIRouter
from ..core.file_preview_manager import FilePreviewManager

router = APIRouter()
preview_manager = FilePreviewManager()

@router.get("")
async def get_preview(file_path: str):
    """
    获取文件预览内容
    
    Args:
        file_path: 文件路径
        
    Returns:
        包含预览内容的响应
    """
    preview_content = preview_manager.get_preview(file_path)
    if preview_content:
        return {
            "status": "success",
            "data": {
                "file_path": preview_content.file_path,
                "file_type": preview_content.file_type,
                "preview_data": preview_content.preview_data,
                "file_size": preview_content.file_size,
                "modified_time": preview_content.modified_time
            }
        }
    return {"status": "error", "message": "Failed to generate preview"}

@router.get("/directory")
async def get_directory_content(directory_path: str):
    """
    获取目录内容的预览
    
    Args:
        directory_path: 目录路径
        
    Returns:
        包含目录下所有文件预览的响应
    """
    contents = preview_manager.get_directory_content(directory_path)
    return {
        "status": "success",
        "data": [
            {
                "file_path": content.file_path,
                "file_type": content.file_type,
                "preview_data": content.preview_data,
                "file_size": content.file_size,
                "modified_time": content.modified_time
            }
            for content in contents
        ]
    } 